package kontrol;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.DALException;
import database.MySQLRaavareBatchDAO;
import database.RaavareBatchDTO;
import kontrol.Validator;

/**
 * Servlet implementation class Rawbatch
 */
@WebServlet({ "/Rawbatch", "/Rawbatch/*" })
public class Rawbatch extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	private Validator validator;
	private RaavareBatchDTO dto;
	private MySQLRaavareBatchDAO dao;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Rawbatch() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// OperatoerDTO login = (OperatoerDTO) session.getAttribute("login");
		// if (login == null) {
		// request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,
		// response);
		// return;
		// } else {
		if (request.getRequestURI().endsWith("Rawbatch/AddRawbatch")) {
			request.setAttribute("message", " ");
			request.getRequestDispatcher("/WEB-INF/rawbatch/createrawbatch.jsp").forward(request, response);
			
		} else if (request.getRequestURI().endsWith("Rawbatch/ShowRawbatch")) {
			formatRawBatchList(request);
			request.getRequestDispatcher("/WEB-INF/rawbatch/showrawbatch.jsp").forward(request, response);
			
		} else if (request.getRequestURI().endsWith("Rawbatch/RawbatchHome")) {
			request.getRequestDispatcher("/WEB-INF/rawbatch/rawbatchhome.jsp").forward(request, response);
		}
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String data = request.getParameter("newBatch");

		if (data != null) {

			validator = new Validator();

			String udloebsdato = request.getParameter("udloebsdato");
			String[] resultUdloebsdato = validator.validateDATE(udloebsdato, "expiring date");

			String maengde = request.getParameter("maengde");
			String[] resultMaengde = validator.validateDOUBLE(maengde, "ammount");

			String raavareId = request.getParameter("raavareId");
			String[] resultReceptId = validator.validateHELTAL(raavareId, "raw Id", 1, 99999999);

			ArrayList<String[]> results = new ArrayList<String[]>();
			ArrayList<String[]> errors = new ArrayList<String[]>();

			results.add(resultUdloebsdato);
			results.add(resultMaengde);
			results.add(resultReceptId);

			for (int i = 0; i < results.size(); i++) {
				if (results.get(i)[2] != null)
					errors.add(results.get(i));
			}

			if (errors.isEmpty()) {
				int rawId = Integer.parseInt(request.getParameter("raavareId"));
				double ammount = Double.parseDouble(request
						.getParameter("maengde"));
				String date = request.getParameter("udloebsdato");
				createRawbatch(rawId, ammount, date);
				request.setAttribute("succes", "Rawbatch created!");
			}
			else {
				request.setAttribute("errors", results);
			}
		}
		request.getRequestDispatcher("/WEB-INF/rawbatch/createrawbatch.jsp").forward(request, response);
	}
	
	
	public void formatRawBatchList(HttpServletRequest request)
			throws ServletException, IOException {

		MySQLRaavareBatchDAO rawbatchDAO = null;
		try {
			rawbatchDAO = new MySQLRaavareBatchDAO();
			request.setAttribute("count", rawbatchDAO.getCount());
		} catch (DALException e) {
			e.printStackTrace();
			request.setAttribute("count", 0);
		}
		if (((Integer) request.getAttribute("count")) < 10) {
			try {
				List<RaavareBatchDTO> liste = rawbatchDAO.getRaavareBatchList();
				String results = "<table border=\"1\"> <tr> <th>RawBatchId </th> <th>RawMatId</th> <th>Ammount</th> <th>Expiring Date</th> </tr>";
				for (int i = 0; i < liste.size(); i++) {
					results += "<tr> <td>" + liste.get(i).getRbId()
							+ "</td><td> " + liste.get(i).getRaavareId()
							+ "</td><td> " + liste.get(i).getMaengde()
							+ "</td><td> " + liste.get(i).getUdloebsDato()
							+ "<br>" + "</td> </tr>";

				}
				results += "</table>";
				request.setAttribute("results", results);
			} catch (DALException e) {

				e.printStackTrace();
			}
		} else {
			if (request.getAttribute("page") == null) {
				request.setAttribute("page", 0);
			}
			if (request.getParameter("p") != null) {
				request.setAttribute("page",
						Integer.parseInt(request.getParameter("p")));
			}
			int curPage = (Integer) request.getAttribute("page");
			try {
				List<RaavareBatchDTO> liste = rawbatchDAO.getRaavareBatchList(curPage, 10);
				String results = "<table border=\"1\"> <tr> <th>RawBatchId </th> <th>RawMatId</th> <th>Ammount</th> <th>Expiring Date</th> </tr>";
				for (int i = 0; i < liste.size(); i++) {

					results += "<tr> <td>" + liste.get(i).getRbId()
							+ "</td><td> " + liste.get(i).getRaavareId()
							+ "</td><td> " + liste.get(i).getMaengde()
							+ "</td><td> " + liste.get(i).getUdloebsDato()
							+ "<br>" + "</td> </tr>";
				}
				results += "</table>";
				if (curPage > 9) {
					results += "<a href=\"?p=" + (curPage - 10)
							+ "\">Prev</a>&nbsp;&nbsp;&nbsp;";
				}
				if ((((Integer) request.getAttribute("count")) - 10) > curPage) {
					results += "<a href=\"?p=" + (curPage + 10) + "\">Next</a>";
				}

				request.setAttribute("results", results);
			} catch (DALException e) {

				e.printStackTrace();
			}
		}
	}
	
	

	private void createRawbatch(int raavareId, double maengde,
			String udloebsdato) {
		dto = new RaavareBatchDTO(0, raavareId, maengde, udloebsdato);
		try {
			dao = new MySQLRaavareBatchDAO();
			dao.createRaavareBatch(dto);
		} catch (DALException e1) {
			e1.printStackTrace();
		}
	}
}
